home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************************
- *
- *
- * ObjectMacZapp -- a standard Mac OOP application template
- *
- *
- *
- * ZTextWindow.cpp -- a window that displays text files (uses TextEdit)
- *
- *
- *
- *
- *
- * © 1996, Graham Cox
- *
- *
- *
- *
- *************************************************************************************************/
-
-
-
- #include "ZTextWindow.h"
- #include "MacZoop.h"
-
-
- ZTextWindow::ZTextWindow(ZCommander* aBoss, short windID, Boolean allowEditing)
- : ZScroller(aBoss, windID, TRUE, TRUE)
- {
- itsText = NULL;
- isEditable = allowEditing;
- }
-
-
-
- ZTextWindow::~ZTextWindow()
- {
- if (itsText)
- TEDispose(itsText);
- }
-
-
- void ZTextWindow::InitZWindow()
- {
- inherited::InitZWindow();
-
- MakeTextEdit();
- }
-
- void ZTextWindow::DrawContent()
- {
- Rect r;
-
- SetOrigin( 0, 0 );
- GetContentRect(&r);
- ClipRect( &r );
- TEUpdate(&r, itsText);
- }
-
- void ZTextWindow::ClickContent(Point mouse, short modifiers)
- {
- if (isEditable)
- {
- SetOrigin( 0, 0 );
- TEClick(mouse, (modifiers & shiftKey), itsText);
- }
- }
-
- void ZTextWindow::Activate()
- {
- inherited::Activate();
- TEActivate(itsText);
- }
-
- void ZTextWindow::Deactivate()
- {
- TEDeactivate(itsText);
- inherited::Deactivate();
- }
-
-
- void ZTextWindow::Idle()
- {
- if (isEditable)
- TEIdle( itsText);
- }
-
-
- void ZTextWindow::AdjustCursor( const Point mouse, const short modifiers )
- {
- Rect content;
- CursHandle aCursor;
-
- if ( isEditable )
- {
- GetContentRect( &content );
-
- if (PtInRect( mouse, &content ))
- {
- aCursor = GetCursor( iBeamCursor );
- SetCursor( *aCursor );
- }
- else
- SetCursor( &qd.arrow );
- }
- }
-
-
- void ZTextWindow::UpdateMenus()
- {
- if ( isEditable )
- {
- if ((*itsText)->selStart < (*itsText)->selEnd )
- {
- gMenuBar->EnableCommand( kCmdCut );
- gMenuBar->EnableCommand( kCmdCopy );
- gMenuBar->EnableCommand( kCmdClear );
- }
-
- gMenuBar->EnableCommand( kCmdSelectAll );
- }
-
- inherited::UpdateMenus();
- }
-
-
-
- Boolean ZTextWindow::CanPasteType()
- {
- return isEditable && gClipboard->QueryType( 'TEXT' );
- }
-
-
- void ZTextWindow::DoCut()
- {
- if ( isEditable )
- {
- gClipboard->Clear();
- TECut( itsText );
- TEToScrap();
- RecalText();
- }
- }
-
-
- void ZTextWindow::DoCopy()
- {
- if ( isEditable )
- {
- gClipboard->Clear();
- TECopy( itsText );
- TEToScrap();
- }
- }
-
-
- void ZTextWindow::DoPaste()
- {
- if ( isEditable )
- {
- TEFromScrap();
- TEPaste( itsText );
- RecalText();
- }
- }
-
-
- void ZTextWindow::DoClear()
- {
- if ( isEditable )
- {
- TEDelete( itsText );
- RecalText();
- }
- }
-
-
- void ZTextWindow::DoSelectAll()
- {
- if ( isEditable )
- TESetSelect( 0, 32767, itsText );
-
- }
-
-
-
- void ZTextWindow::SetSize(short width, short height)
- {
- inherited::SetSize( width, height );
- RecalText();
- }
-
-
- void ZTextWindow::Zoom(short partCode)
- {
- inherited::Zoom(partCode);
- RecalText();
- Draw();
- }
-
-
-
- void ZTextWindow::Scroll(short dH, short dV)
- {
- TEPinScroll(dH, dV, itsText);
- }
-
-
-
- void ZTextWindow::Type(char theChar)
- {
- if (isEditable)
- {
- TEKey(theChar, itsText);
-
- dirty = TRUE;
- }
- }
-
-
- void ZTextWindow::MakeTextEdit()
- {
- Rect destRect, viewRect;
-
- // the dest rect is the scrollbounds, but that hasn't been set yet.
- // the view rect is the content area
-
- Focus();
-
- TextFont(monaco);
- TextSize(9);
-
- emSpace = CharWidth('m');
- GetContentRect(&viewRect);
-
- destRect = viewRect;
- destRect.right = destRect.left + (emSpace * 255);
-
- FailNIL(itsText = TEStyleNew(&destRect, &viewRect));
-
- SetBounds( destRect );
- RecalText();
- }
-
-
-
- void ZTextWindow::GetContentRect(Rect* contents)
- {
- inherited::GetContentRect(contents);
-
- contents->top += 2;
- contents->left += 2;
- }
-
-
- void ZTextWindow::OpenFile(OSType fType)
- {
- FInfo fi;
- short refNum;
- long pSize;
- Handle temp = NULL;
-
- if (macFile.vRefNum != kNoFile)
- {
- FailOSErr(FSpGetFInfo(&macFile, &fi));
-
- if (fi.fdType != 'TEXT')
- FailOSErr(paramErr);
-
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
-
- try
- {
- FailOSErr(GetEOF(refNum, &pSize));
- if (pSize > kMaxTextSize)
- FailOSErr(kTextFileTooBigErr);
-
- FailNIL(temp = NewHandle(pSize));
-
- HLock(temp);
- FailOSErr(FSRead(refNum, &pSize, *temp));
-
- // set the text in text edit
-
- TESetText(*temp, pSize, itsText);
- HUnlock(temp);
-
- DisposeHandle(temp);
- FSClose(refNum);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
-
- if (temp)
- {
- HUnlock(temp);
- DisposeHandle(temp);
- }
- throw err;
- }
- }
- inherited::OpenFile( fType );
-
- // set up the bounds, etc
-
- RecalText();
- Draw();
- }
-
-
-
- void ZTextWindow::SaveFile()
- {
- short refNum;
- long pSize;
- OSErr theErr;
- Handle text = NULL;
- char tState;
-
- if (macFile.vRefNum != kNoFile)
- {
- theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
-
- if (theErr == fnfErr)
- {
- FailOSErr(FSpCreate(&macFile, gAppSignature, 'TEXT', 0));
- FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
- }
- else
- FailOSErr(theErr);
-
- try
- {
- FailNIL(text = (*itsText)->hText);
-
- pSize = GetHandleSize(text);
-
- tState = HGetState(text);
- HLock(text);
- FailOSErr(FSWrite(refNum, &pSize, *text));
- HSetState(text, tState);
-
- FailOSErr(SetEOF(refNum, pSize));
- FSClose(refNum);
- }
- catch(OSErr err)
- {
- FSClose(refNum);
- if (text)
- HSetState(text, tState);
- throw err;
- }
- inherited::SaveFile();
- }
- }
-
-
- void ZTextWindow::RecalText()
- {
- static Boolean rtInProgress = FALSE;
-
- Rect content,tBounds;
- short textHeight;
- short lineHeight;
-
- if ( ! rtInProgress )
- {
- rtInProgress = TRUE;
-
- GetContentRect(&content);
- (*itsText)->viewRect = content;
-
- TECalText(itsText);
- lineHeight = TEGetHeight(1, 1, itsText);
- textHeight = TEGetHeight(0, 32767 , itsText);
-
- GetBounds(&tBounds);
- tBounds.bottom = textHeight + lineHeight;
- SetBounds( tBounds );
-
- (*itsText)->destRect = tBounds;
- SetScrollAmount( emSpace, lineHeight);
-
- rtInProgress = FALSE;
- }
- }
-